package com.example.backed.controller;

import com.example.backed.entity.GroupBuy;
import com.example.backed.service.GroupBuyService;
import com.example.backed.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "公共团购接口")
@RestController
@RequestMapping("/api/group-buy")
@RequiredArgsConstructor
@Slf4j
public class GroupBuyPublicController {

    private final GroupBuyService groupBuyService;

    @Operation(summary = "获取进行中的团购活动")
    @GetMapping("/ongoing")
    public Result<List<GroupBuy>> getOngoingGroupBuys() {
        log.info("获取进行中的团购活动");
        List<GroupBuy> list = groupBuyService.findOngoing();
        return Result.success(list);
    }

    @Operation(summary = "获取即将开始的团购活动")
    @GetMapping("/upcoming")
    public Result<List<GroupBuy>> getUpcomingGroupBuys() {
        log.info("获取即将开始的团购活动");
        List<GroupBuy> list = groupBuyService.findUpcoming();
        return Result.success(list);
    }

    @Operation(summary = "获取已结束的团购活动")
    @GetMapping("/ended")
    public Result<List<GroupBuy>> getEndedGroupBuys() {
        log.info("获取已结束的团购活动");
        // 查询状态为已结束的团购活动
        List<GroupBuy> list = groupBuyService.findByStatus(2);
        return Result.success(list);
    }

    @Operation(summary = "获取团购活动详情")
    @GetMapping("/{id}")
    public Result<GroupBuy> getGroupBuyDetail(@Parameter(description = "团购ID") @PathVariable Long id) {
        log.info("获取团购活动详情: {}", id);
        GroupBuy groupBuy = groupBuyService.getGroupBuyDetail(id);
        return Result.success(groupBuy);
    }
} 